package com.xiaozhengkeji.mc.xzlib.tools;

import com.comphenix.protocol.utility.StreamSerializer;
import org.bukkit.inventory.ItemStack;

public class ItemUtils {
    public static ItemStack getItemStack(String data) {
        StreamSerializer ss = new StreamSerializer();
        try {
            return ss.deserializeItemStack(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String toData(ItemStack item) {
        StreamSerializer ss = new StreamSerializer();
        try {
            return ss.serializeItemStack(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Item转Json
     */
    public static String itemToJson(ItemStack is) {
        if (is == null) {
            return "null";
        }
        return toData(is);
    }

    /**
     * Json转Item
     */
    public static ItemStack jsonToItem(String json) {
        if (json.equalsIgnoreCase("null")) {
            return null;
        }
        return getItemStack(json);
    }

}
